package com.sea.demo.shiro.exception;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author: 快乐在角落里
 * @Create: 2020/11/7 23:07
 * Shiro异常处理器
 */
@RestControllerAdvice
public class ShiroExceptionHandler {

    private  final static Logger LOGGER = LogManager.getLogger(ShiroExceptionHandler.class);
    /**
     * 没有权限
     */
    @ExceptionHandler(AuthorizationException.class)
    public String unauthorizedException(AuthorizationException e){
        LOGGER.warn("{} --> {} --> {}",e.getClass(),e.getMessage(),"没有权限！");
        return "没有权限";
    }

    /**
     * 密码错误
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    public String incorrectCredentialsException(IncorrectCredentialsException e){
        LOGGER.warn("{} --> {} --> {}",e.getClass(),e.getMessage(),"密码错误！");
        return "密码错误";
    }

    /**
     * 用户已被冻结
     */
    @ExceptionHandler(LockedAccountException.class)
    public String lockedAccountException(LockedAccountException e){
        LOGGER.warn("{} --> {} --> {}",e.getClass(),e.getMessage(),"用户已被冻结！");
        return "用户已被冻结";
    }

    /**
     * 用户不存在
     */
    @ExceptionHandler(UnknownAccountException.class)
    public String unknownAccountException(UnknownAccountException e){
        LOGGER.warn("{} --> {} --> {}",e.getClass(),e.getMessage(),"用户不存在！");
        return "用户不存在";
    }
}
